package org.kazao.calendar;

import java.awt.Dimension;
import java.util.Calendar;
import javax.swing.JButton;

public final class KazaoCalendarDialog extends javax.swing.JDialog {
    public static int COMMAND_APPROVE = 0;
    public static int COMMAND_CANCEL = 1;
    private int result;
    
    public KazaoCalendarDialog() {
        initComponents();
        Dimension screenSize = getToolkit().getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-getWidth())/2, (screenSize.height-getHeight())/2, getWidth(), getHeight());
        setModal(true);
        
        calendar.addChangeListener(new KazaoCalendarAdapter() {
            public void onChange(Calendar calendar) {
            }
            public void onChange(KazaoCalendar calendar) {
            }
            public void onDoubleClick() {
                result = COMMAND_APPROVE;
                dispose();
            }
        });
    }
    
    public KazaoCalendarDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    public int showCalendar() {
        result = COMMAND_CANCEL;
        setVisible(true);
        return result;
    }
    
    public int showCalendar(Calendar calendar) {
        this.calendar.setCalendar(calendar);
        return showCalendar();
    }
    
    public int showCalendar(String date) {
        try {
            this.calendar.setDateTime(date, getLocalize().getShortDateFormat());
        } catch (KazaoCalendarDateTimeException ex) {
            ex.printStackTrace();
        }
        return showCalendar();
    }
    
    public int showCalendar(String date, String format) {
        try {
            this.calendar.setDateTime(date, format);
        } catch (KazaoCalendarDateTimeException ex) {
            ex.printStackTrace();
        }
        return showCalendar();
    }
    
    public static Calendar showAndGetCalendar() {
        Calendar result = null;
        KazaoCalendarDialog cal = new KazaoCalendarDialog();
        if (cal.showCalendar() == COMMAND_APPROVE) {
            result = cal.getCalendar();
        }
        return result;
    }
    
    public static KazaoCalendar showAndGetKazaoCalendar() {
        KazaoCalendar result = null;
        KazaoCalendarDialog cal = new KazaoCalendarDialog();
        if (cal.showCalendar() == COMMAND_APPROVE) {
            result = cal.getKazaoCalendar();
        }
        return result;
    }
    
    public Calendar getCalendar() {
        return calendar.getCalendar();
    }
    
    public KazaoCalendarLocal getLocalize() {
        return calendar.getLocalize();
    }
    
    public KazaoCalendar getKazaoCalendar() {
        return calendar;
    }
    
    public String getDate() {
        return calendar.getDate(getLocalize().getShortDateFormat());
    }
    
    public String getDate(String format) {
        return calendar.getDate(format);
    }
    
    public JButton getApproveButton() {
        return cApprove;
    }
    
    public JButton getCancelButton() {
        return cCancel;
    }
    
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        pCalendar = new javax.swing.JPanel();
        calendar = new org.kazao.calendar.KazaoCalendar();
        cApprove = new javax.swing.JButton();
        cCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Calendar");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        pCalendar.setLayout(null);

        pCalendar.setPreferredSize(new java.awt.Dimension(226, 214));
        pCalendar.add(calendar);
        calendar.setBounds(6, 6, 215, 165);

        cApprove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/kazao/calendar/images/aprove.png")));
        cApprove.setMnemonic('o');
        cApprove.setText("OK");
        cApprove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cApproveActionPerformed(evt);
            }
        });

        pCalendar.add(cApprove);
        cApprove.setBounds(6, 178, 80, 25);

        cCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/kazao/calendar/images/cancel.png")));
        cCancel.setMnemonic('c');
        cCancel.setText("Cancel");
        cCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cCancelActionPerformed(evt);
            }
        });

        pCalendar.add(cCancel);
        cCancel.setBounds(126, 178, 95, 25);

        getContentPane().add(pCalendar, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        dispose();
    }//GEN-LAST:event_formWindowClosing
    
    private void cApproveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cApproveActionPerformed
        result = COMMAND_APPROVE;
        dispose();
    }//GEN-LAST:event_cApproveActionPerformed
    
    private void cCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cCancelActionPerformed
        dispose();
    }//GEN-LAST:event_cCancelActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cApprove;
    private javax.swing.JButton cCancel;
    private org.kazao.calendar.KazaoCalendar calendar;
    public javax.swing.JPanel pCalendar;
    // End of variables declaration//GEN-END:variables
    
}
